package net.sudot.chess.business.model;


import javax.persistence.Column;
import javax.persistence.MappedSuperclass;

/**
 * 排序基类
 *
 * @author tangjialin on 2018-03-18.
 */
@MappedSuperclass
public abstract class BaseOrderedEntity<E extends BaseOrderedEntity> extends BaseEntity<E> implements Comparable<BaseOrderedEntity> {

    /** "排序"属性名称 */
    public static final String ORDER_PROPERTY_NAME = "order";

    /** 排序 */
    @Column(name = "orders")
    private Integer order;

    public Integer getOrder() {
        return order == null ? 1 : order;
    }

    @SuppressWarnings("unchecked")
    public E setOrder(Integer order) {
        this.order = order == null ? 1 : order;
        return (E) this;
    }

    /**
     * 实现compareTo方法
     *
     * @param orderEntity 排序对象
     * @return 比较结果
     */
    @Override
    public int compareTo(BaseOrderedEntity orderEntity) {
        if (orderEntity == null) {
            return 1;
        }
        return getOrder().compareTo(orderEntity.getOrder());
    }

}